"""
命名空间：
    tns:"http://service.jgmes.com"
过站口：GetcompleteInfo
输入：GetcompleteInfoRequest
    消息格式：
    tns:GetcompleteInfo
        barCode:string                  # 产品SN号
        order:string                    # ???
输出：GetcompleteInfoResponse
    消息格式：
    tns:GetcompleteInfoResponse
        out:            # 格式：tns:ResultBoolean
            data:bool           # 过站成功为True
            errorCode:int       # 错误代码，暂时没见过
            isSuccess:bool      # 过站成功为True
            message:str         # 过站失败，会返回原因。过站成功，则无返回值。


返回值（固定模版）：
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
        <ns1:GetcompleteInfoResponse xmlns:ns1="http://service.jgmes.com">
            <ns1:out>
                <data xmlns="http://service.jgmes.com">false</data>
                <errorCode xmlns="http://service.jgmes.com" xsi:nil="true" />
                <isSuccess xmlns="http://service.jgmes.com">false</isSuccess>
                <message xmlns="http://service.jgmes.com">本工序已扫，请检查！已做到第[4]工序[VX989-成品全功能测试站（ATS）]</message>
            </ns1:out>
        </ns1:GetcompleteInfoResponse>
    </soap:Body>
</soap:Envelope>
"""
import time
from sys import flags
from tkinter.messagebox import showinfo

import requests
import re
import xml.etree.ElementTree as ET
import queue

url = 'http://192.168.0.7:8080//services/GZJDDeviceService'
#
# url = 'http://10.168.3.8:8080/services/GZJDDeviceService'   # 益智

headers = {
    'Content-Type':'text/xml'
}

# barCode = 'VG0015-BO0200349'
barCode = "VG0061-FP0000068"
# BTTestData = ""             # 测试用例的不良项
# TestResults = 'x'        # 测试结果
orderStationId = '6'
body = f'''
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:ser="http://service.jgmes.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:GetcompleteInfo>
         <barCode>{barCode}</barCode>
         <order>{orderStationId}</order>
      </ser:GetcompleteInfo>
   </soapenv:Body>
</soapenv:Envelope>
'''

# res = requests.post(url,headers=headers,data=body)
# print(res.status_code)
# print(res.text)



# import tkinter
# from tkinter.messagebox import showwarning,showinfo





# 函数接口
def GetcompleteInfo(url=url, barCode='', stationId=0):
    flag = False
    body = f'''
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                     xmlns:ser="http://service.jgmes.com/">
       <soapenv:Header/>
       <soapenv:Body>
          <ser:GetcompleteInfo>
             <barCode>{barCode}</barCode>
             <order>{stationId}</order>
          </ser:GetcompleteInfo>
       </soapenv:Body>
    </soapenv:Envelope>
    '''
    res = requests.post(url, headers=headers, data=body)
    res_text = res.text
    print(res_text)
    try:
        message = re.findall(r"<message.*>(.*?)</message>", res_text)
        s = message[0][:2]
        if s == 'OK':
            print('工站正确')
            flag = True
            # result = showinfo('提示', '错误信息,\n目前工站正确')
        elif s == 'NG':
            print(f'工站错误,目前到的工站流程为{message[0][4:]}')
            # result = showwarning('警告', '错误信息,\n目前工站流程为' + message[0][4:])
            result = showinfo('提示', '错误信息,\n目前工站流程为' + message[0][4:])
            # print(f'提示: {result}')    # 警告：ok
    except Exception as e:
        flag = False
    return flag

if __name__ == '__main__':
    # window = tkinter.Tk()
    # window.withdraw()  # 隐藏主窗口
    print('123123')
    GetcompleteInfo(url=url, barCode=barCode, stationId=2)
    print('1111')
    time.sleep(3)
    input()